package com.feishi.core.util.bitbyte;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: caixq
 * Date: 2019-06-17
 * Time: 下午5:21
 */
public class ReadAndWrightDemo {

    public static void main(String[] args) throws IOException {

        //加载协议配置
        List<Map<String, String>> configMap = CsvUtil.readListWithTitle(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.csv"));
        List<ByteUtil.BitByteConfig> bitByteConfigs=new ArrayList<>();
        for (Map<String, String> map : configMap) {
            System.out.println(map);
            ByteUtil.BitByteConfig bitByteConfig=new ByteUtil.BitByteConfig();
            String name= map.get("name");
            String startBit= map.get("startBit");
            String size= map.get("size");
            String factor= map.get("factor");
            String offset= map.get("offset");
            String initialValue= map.get("initialValue");
            bitByteConfig.name=name;
            bitByteConfig.startBit=Integer.parseInt(startBit);
            bitByteConfig.size=Integer.parseInt(size);
            bitByteConfig.factor=factor==null?1:Double.parseDouble(factor);
            bitByteConfig.offset=factor==null?1:Double.parseDouble(offset);
            bitByteConfig.initialValue=initialValue==null?0:Double.parseDouble(initialValue);
            bitByteConfigs.add(bitByteConfig);
        }


        //构建封包工具
        ByteUtil.BitByteBuild build=new ByteUtil.BitByteBuild(bitByteConfigs);
        build.setValue("vol",22);//此操作根据csv文件中每个消息的key值直接设置对应信息
        build.setValue("soc",50);//此操作根据csv文件中每个消息的key值直接设置对应信息
        build.setValue("sendTime",(int)(System.currentTimeMillis()/1000)); //由于时间由gps接收时不方便获取时间戳,直接转换成数据 数据源19-6-14 17:50:31
        build.setValue("status3",1);

        System.out.println((int)(System.currentTimeMillis()/1000));
        build.buildContent();//获取封包后的数据
        System.out.println(ByteUtil.getHexString(build.buildContent()));

        //构建解包工具
        ByteUtil.BitByteReader bitByteReader=new ByteUtil.BitByteReader(bitByteConfigs,build.buildContent(),ByteUtil.BitByteConfig.MOTOROLA);


        System.out.println(bitByteReader.getDouble("vol"));//获取实际值
        System.out.println(bitByteReader.getDouble("soc"));//获取实际值
        System.out.println(bitByteReader.getInt("vol"));//获取传输中间值
        System.out.println(bitByteReader.getInt("status1"));//获取传输中间值
        System.out.println(bitByteReader.getInt("status2"));//获取传输中间值
        System.out.println(bitByteReader.getInt("status3"));//获取传输中间值
        int sendTime=bitByteReader.getInt("sendTime");
        System.out.println(sendTime);
        System.out.println(new Date(sendTime*1000l).toLocaleString());



    }
}
